<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2018/12/3
 * Time: 17:11
 */

namespace common\sources\read\member;

use common\models\craftsman\BuyUserBankAccount;
use common\models\craftsman\ClassifyReason;
use common\models\craftsman\Member;;
class MemberAccess implements IMemberAccess
{
    public function getList($data,$count = false){

        $where = "1=1";
        if(!empty($data['role'])){
            $where .= " and role =".$data['role'];
        }

        if(!empty($data['search_type'])){
            if($data['search_type'] == 1 && !empty($data['name'])){
                $where .= " and phone =".$data['name'];
            }else if($data['search_type'] == 2 && !empty($data['name'])){
                $where .= " and inviter_phone =".$data['name'];
            }else if($data['search_type'] == 3 && !empty($data['name'])){
                $where .= " and qq =".$data['name'];
            }else if($data['search_type'] == 4 && !empty($data['name'])){
                $where .= " and wx =".$data['name'];
            }else if($data['search_type'] == 5 ){
	            $where .= " and id in (".$data['name'].")";
            }
        }

        if (!$count) {
            return Member::find()
                ->select('id,phone,pid,reg_time,expire_time,send_receive_time,qq,wx,inviter_phone,role,login_time')
                ->where($where)
                ->orderBy("reg_time desc")
                ->offset($data['limit']*($data['page']-1))
                ->limit($data['limit'])
                ->asArray()
                ->all();
        }
        return Member::find()
            ->select('id,phone,pid,reg_time,expire_time,send_receive_time,qq,wx,inviter_phone,role,login_time')
            ->where($where)
            ->asArray()
            ->count();
    }

    public function getSignMember($role,$search) {
        return Member::find()
            ->select("id,phone,reg_time,expire_time,send_receive_time,qq,wx,inviter_phone,role,login_time")
            ->where(["phone" => $search,"role"=>$role])
            ->asArray()
            ->all();
    }

    //修改过期时间
    public function modifyExpireTime($data){
        $user = Member::findOne(['id' => $data['buy_id']]);
        if ($user) {
            $user->expire_time = $data['expire_time'];
            if ($user->save()) {
                return true;
            }
        }
        return false;
    }


	/**
	 * getUserInfo 根据ID 获取用户信息
	 * @param $id
	 * @author 中崋 (1291713293@qq.com)
	 * @date   2019/5/20
	 * @return array
	 */
	public function getUserInfo($id) {
		return Member::find()
			->select("id,phone,pid,reg_time,expire_time,send_receive_time,qq,wx,inviter_phone,role,login_time")
			->where(["id" => $id])
			->asArray()
			->one();
	}


	/**
	 * newGetList 用于导出
	 * @param $data
	 * @author 中崋 (1291713293@qq.com)
	 * @date   2019/5/20
	 * @return int|string
	 */
	public function newGetList($data) {

		$where = "1=1";
		if (!empty($data['role'])) {
			$where .= " and role =" . $data['role'];
		}
		if (!empty($data['ids'])) {
			$where .= " and id in( " . $data['ids'].") ";
		}

		return Member::find()
			->select('id,phone,pid,reg_time,expire_time,send_receive_time,qq,wx,inviter_phone,role,login_time')
			->where($where)
			->asArray()
			->all();
	}
}